package com.jn.zhinongbao.adapter.utils;

import android.content.Context;

import com.example.supportingagriculturaltreasures.bean.ChatMessage;
import com.example.supportingagriculturaltreasures.bean.ChatRoom;
import com.example.supportingagriculturaltreasures.bean.User;
import com.example.supportingagriculturaltreasures.database.DatabaseHelper;

import java.util.List;

/**
 * 聊天管理器 - 使用数据库存储聊天数据
 */
public class ChatManager {
    private static ChatManager instance;
    private DatabaseHelper databaseHelper;

    private ChatManager(Context context) {
        databaseHelper = new DatabaseHelper(context);
    }

    public static synchronized ChatManager getInstance(Context context) {
        if (instance == null) {
            instance = new ChatManager(context.getApplicationContext());
        }
        return instance;
    }

    /**
     * 创建或获取聊天室
     * @param userId 用户ID
     * @param merchantId 商家ID
     * @return 聊天室ID
     */
    public String createOrGetChatRoom(int userId, int merchantId) {
        // 获取用户和商家信息
        User user = databaseHelper.getUserById(userId);
        User merchant = databaseHelper.getUserById(merchantId);
        
        if (user != null && merchant != null) {
            return databaseHelper.createOrGetChatRoom(userId, merchantId, user.getUsername(), merchant.getUsername());
        }
        
        return null;
    }

    /**
     * 发送消息
     * @param chatRoomId 聊天室ID
     * @param senderId 发送者ID
     * @param content 消息内容
     * @return 是否发送成功
     */
    public boolean sendMessage(String chatRoomId, int senderId, String content) {
        User sender = databaseHelper.getUserById(senderId);
        if (sender != null) {
            return databaseHelper.sendMessage(chatRoomId, senderId, sender.getUsername(), content);
        }
        return false;
    }

    /**
     * 获取聊天室列表
     * @param currentUserId 当前用户ID
     * @return 聊天室列表
     */
    public List<ChatRoom> getChatRoomList(int currentUserId) {
        return databaseHelper.getChatRoomsByUserId(currentUserId);
    }

    /**
     * 获取聊天消息列表
     * @param chatRoomId 聊天室ID
     * @param currentUserId 当前用户ID
     * @return 消息列表
     */
    public List<ChatMessage> getChatMessages(String chatRoomId, int currentUserId) {
        return databaseHelper.getChatMessages(chatRoomId, currentUserId);
    }

    /**
     * 标记消息为已读
     * @param chatRoomId 聊天室ID
     * @param currentUserId 当前用户ID
     */
    public void markMessagesAsRead(String chatRoomId, int currentUserId) {
        databaseHelper.markMessagesAsRead(chatRoomId, currentUserId);
    }

    /**
     * 获取未读消息总数
     * @param currentUserId 当前用户ID
     * @return 未读消息总数
     */
    public int getTotalUnreadCount(int currentUserId) {
        List<ChatRoom> chatRooms = getChatRoomList(currentUserId);
        int totalCount = 0;
        for (ChatRoom chatRoom : chatRooms) {
            totalCount += chatRoom.getUnreadCount();
        }
        return totalCount;
    }

    /**
     * 删除指定聊天室
     * @param chatRoomId 聊天室ID
     */
    public void deleteChatRoom(String chatRoomId) {
        databaseHelper.deleteChatRoom(chatRoomId);
    }
} 